package com.navi.loaddata.loadService.impl.hisData.array;

import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.mesEntity.MesCtProductlocationhist;
import com.navi.loaddata.core.mesEntity.MesProducthistory;
import com.navi.loaddata.core.oracleEntity.array.EtlArrayGlassHstV;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.core.util.ProcessUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jay
 */
@Service
public class ProcessHisDataServiceARRAY {
    @Autowired
    private MesRepositoryImpl mesdao;

    private static final Logger logger = LoggerFactory.getLogger(ProcessHisDataServiceARRAY.class);

    public List<EtlArrayGlassHstV> processHis(Map<String, MesProducthistory> hisTrackIn, MesProducthistory hisOut, int errorProcessArrayGlassHstV) throws ETLException {
        List<EtlArrayGlassHstV> ldrArrayGlassHstTList = new ArrayList<>();
        try {
            EtlArrayGlassHstV etlArrayGlassHstV = new EtlArrayGlassHstV();
            etlArrayGlassHstV.setStepId(hisOut.getOldprocessoperationname());
            etlArrayGlassHstV.setGlassId(hisOut.getProductname());
            etlArrayGlassHstV.setGlassEndTime(hisOut.getEventtime());
            if(null==hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey())){
                throw new ETLException("errorProcessArrayGlassHstV:Linkkey IS NULL ");
            }
            Date glassStartTime = hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey()).getEventtime();
            etlArrayGlassHstV.setGlassStartTime(glassStartTime);
            etlArrayGlassHstV.setProductId(hisOut.getProductspecname());
            etlArrayGlassHstV.setProductVersion(hisOut.getProductspecversion());
            etlArrayGlassHstV.setLotId(ProcessUtil.processString(hisOut.getLotname()));
            etlArrayGlassHstV.setEquipId(hisOut.getMachinename());
            etlArrayGlassHstV.setRecipeId(hisOut.getMachinerecipename());

//        MesCtProductlocationhist mesCtProductlocationhist=new MesCtProductlocationhist();
            String hql = "FROM MesCtProductlocationhist WHERE EVENTNAME='ProductOut' AND TRACELEVEL<>'P' and  LINKKEY='" + hisOut.getLinkkey() + "' and PRODUCTNAME='" + hisOut.getProductname() + "' and SUBFACTORYNAME='" + hisOut.getShipid() + "' order by EVENTTIME ASC";
            String chamberPath = "";
            String chamberTime = "";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<MesCtProductlocationhist> mesCtProductlocationhistList = mesdao.list(MesCtProductlocationhist.class, hql.toString());
            if (mesCtProductlocationhistList.size() > 0) {
                for (int i = 0; i < mesCtProductlocationhistList.size(); i++) {
                    chamberPath = chamberPath + mesCtProductlocationhistList.get(i).getUnitname();
                    chamberTime = chamberTime + sdf.format(mesCtProductlocationhistList.get(i).getEventtime().getTime());
                }
            }
            etlArrayGlassHstV.setChamberPath(chamberPath);
            etlArrayGlassHstV.setChamberTime(chamberTime);
            etlArrayGlassHstV.setTaShift(hisOut.getEventname());
            etlArrayGlassHstV.setRunMode(hisOut.getRunmode());
            etlArrayGlassHstV.setOwnerId(hisOut.getOwnerid());
            etlArrayGlassHstV.setOwnerType(hisOut.getOwnertype());
            //8
            etlArrayGlassHstV.setCstId(hisOut.getCarriername());
            etlArrayGlassHstV.setSlotNo(String.valueOf(hisOut.getPosition()));
            etlArrayGlassHstV.setCstIdMoveIn(hisOut.getCarriername());
            etlArrayGlassHstV.setSlotNoMoveIn(String.valueOf(hisOut.getPosition()));
            //13-14
            etlArrayGlassHstV.setPortIdMoveIn(hisOut.getProcessflowname());
            etlArrayGlassHstV.setPortIdMoveOut(hisOut.getEventuser());
            etlArrayGlassHstV.setMesLinkKry(hisOut.getLinkkey());
            if (hisOut.getProductspecname().length() >= 7) {
                if ("OC".equals(hisOut.getFactoryname())) {
                    if ("L".equals(hisOut.getProductspecname().substring(0, 1))) {
                        etlArrayGlassHstV.setPanelSize(hisOut.getProductspecname().substring(2, 7));
                    } else {
                        etlArrayGlassHstV.setPanelSize(hisOut.getProductspecname().substring(1, 6));
                    }
                } else {
                    etlArrayGlassHstV.setPanelSize(hisOut.getProductspecname().substring(2, 7));
                }
            }
            //19-21
            etlArrayGlassHstV.setHoldFlag(hisOut.getProductholdstate());
            etlArrayGlassHstV.setHoldDept(hisOut.getHolddepartment());
            etlArrayGlassHstV.setQtapId(hisOut.getQtapgroupname());
            etlArrayGlassHstV.setLotPriority(String.valueOf(hisOut.getPriority()));
            etlArrayGlassHstV.setGlassGrade(hisOut.getProductgrade());
            etlArrayGlassHstV.setGlassJudge(hisOut.getProductjudge());
            etlArrayGlassHstV.setPanelGrades(hisOut.getPanelgrades());
            etlArrayGlassHstV.setPanelOxInfo(hisOut.getPaneloxs());
            etlArrayGlassHstV.setNgMark(hisOut.getNgmark());
            etlArrayGlassHstV.setPanelQty(Long.valueOf(getNum(hisOut.getPaneltotalqty())));
            etlArrayGlassHstV.setPanelQtyGood(Long.valueOf(getNum(hisOut.getGoodpanelgradeqty())));
            etlArrayGlassHstV.setDefectCode(hisOut.getDefectcodes());
            etlArrayGlassHstV.setReworkCnt(hisOut.getReworkcount());
            etlArrayGlassHstV.setRecycleCnt(hisOut.getRecyclecount());
            //32
            etlArrayGlassHstV.setExpNo(hisOut.getExpno());
            etlArrayGlassHstV.setTecnId(hisOut.getTecnname());
            etlArrayGlassHstV.setMaskId(hisOut.getMaskname());
            etlArrayGlassHstV.setMtrlPartNo(hisOut.getTftpolpartno());
            etlArrayGlassHstV.setMtrlLotNo(hisOut.getTftpollotname());

            ldrArrayGlassHstTList.add(etlArrayGlassHstV);

        }catch (Exception e){
            logger.error(errorProcessArrayGlassHstV+":"+e.getMessage());
            logger.error(LoggerUtil.stackTraceToString(e));
            throw new ETLException(errorProcessArrayGlassHstV+":"+e.getMessage());
        }
        return ldrArrayGlassHstTList;
    }


    private Long getNum(String str) {
        Long num;
        try {
            num = Long.valueOf(str);
            return num;
        } catch (Exception e) {
//            logger.info("FormatException: BigDecimal[{}] cannot be cast to NUMBER", str);
            num = Long.valueOf(-1);
            return num;
        }
    }
}
